import {Base64} from 'js-base64'
// 设置cookie
export function setCookie (username:string, password:string, days:number) {
    let pwd:string = Base64.encode(password);
    let existDay:Date = new Date();
    // 设置 cookie 的有效期
    existDay.setTime(existDay.getTime() + 24 * 60 * 60 * 1000 * days);
    //字符串拼接cookie
    window.document.cookie =
        "userName" +
        "=" +
        username +
        ";path=/;expires=" +
        existDay.toUTCString();
    window.document.cookie =
        "password" +
        "=" +
        pwd +
        ";path=/;expires=" +
        existDay.toUTCString();
}

// 获取cookie
export function getCookie() {
    let obj = {
        username: '',
        password: ''
    }
    if (document.cookie.length > 0) {
        const arr = document.cookie.split("; "); //这里显示的格式需要切割一下
        for (let i = 0; i < arr.length; i++) {
            const arr2 = arr[i].split("="); //再次切割
            //判断查找相对应的值
            if (arr2[0] == "userName") {
                obj.username = arr2[1];
            } else if (arr2[0] == "password") {
                // Base64解密
                obj.password = Base64.decode(arr2[1]);
            }
        }
    }
    return obj;
}